/**
 * Created by zhangyanbo on 2016/12/22.
 */

import Vue from 'vue';


import tpl from './tpl.html';
import {
    REG_EN_NAME,
    REG_EMAIL,
    REG_PHONE
} from '../../../../grsc-base/common/constants';
import commonService from '../../../common/commonService';
import Help from '../../../../grsc-base/component/Help.vue';
import Loading from '../../../../grsc-base/component/Loading.vue';
import NoData from '../../../../grsc-base/component/NoData.vue';
import Validator from '../../../../grsc-base/component/Validator/main';

import lib from '../../../common/lib';

import idVerify from './component/idVerify/main';
import addrVerify from './component/addrVerify/main';
import Nationality from '../../../component/Nationality.vue';
import DatePickerNew from '../../../../grsc-base/component/DatePicker/vue-datepicker-es6.vue';
import unsupportedCountries from './component/unsupportedCountries/main'

import service from './service';
import config from './config';
export default Vue.extend({
    name: "Certification",
    template: tpl,
    components: {
        Loading,
        Help,
        Validator,
        DatePickerNew,
        idVerify,
        addrVerify,
        Nationality,
        unsupportedCountries,
        NoData
    },
    data() {
        return {
            REG_EMAIL,
            config: config,
            level: '',
            isLoading: true,
            hasBasicAuthed: false, //是否已经填写基本信息
            currPage: 'basic',
            showUnsupportedCountries: false,


            //信息页面参数
            name: '',
            email: '',
            uid: '',
            limit: '',
            limitArr: [],
            isLevelLoading: false,

            userIdentification: {
                auditStatus: config["UNVERIFIED"][this.$i18n.locale],
                auditMessage: "",
            },
            userResidence: {
                auditStatus: config["UNVERIFIED"][this.$i18n.locale],
                auditMessage: "",
            },


            isRequesting: false
        }
    },
    watch: {},
    created() {
        this.init();
    },
    mounted() {},
    methods: {
        init() {
            scrollTo(0, 0);
            this.isLoading = true;

            $.when(service.getBasicAuthInfo(), commonService.basic()).done((authRep, basicRep) => {
                if (authRep.code === window.CODE.SUCCESS) {
                    this.formatAuditMessage(authRep);
                } else {
                    window.error(authRep.msg, null, 4000);
                }

                if (basicRep.code === window.CODE.SUCCESS) {
                    this.uid = lib.getFormattedUid(basicRep.data.uid);
                    this.email = basicRep.data.account;
                    this.name = basicRep.data.name;
                    this.level = basicRep.data.authLevel;
                    this.fetchWithdrawConfig();
                }
                this.isLoading = false;
            });
        },

        goBack() {
            this.init();
            this.currPage = 'basic';

        },

        gotoPage(page) {
            this.currPage = page;
        },

        fetchWithdrawConfig() {
            this.isLevelLoading = true;
            let limitArr = [];
            commonService.fetchAssets((assetMap, assetList) => {
                this.isLevelLoading = false;
                let results = [];
                // 仅仅显示status = LISTED的数组
                $.map(assetList, (item, index) => {
                    if (item.status === 'LISTED') {
                        results.push(item);
                    }
                });
                this.limitArr = results;
            });

            /*
            commonService.assetConfigList({key:'WITHDRAWLEVEL_0'}).done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    let limitArr = [];
                    let limitValue = '';
                    // console.log(rep.data)
                    rep.data.forEach((item, index) => {
                        if (item.profileValue > 0) {
                            limitValue = item.profileValue;
                        } else if (item.profileValue == 0) {
                            limitValue = this.$t('basic.nolimit');
                        } else if (item.profileValue == -1) {
                            limitValue = this.$t('basic.noout');
                        }
                        limitArr.push({assetCode: item.assetCode, level0: limitValue});
                    });
                    commonService.assetConfigList({key:'WITHDRAWLEVEL_1'}).done((rep) => {
                        if (rep.code === window.CODE.SUCCESS) {
                            rep.data.forEach((item, index) => {
                                for(let name in limitArr){
                                    if(limitArr[name].assetCode === item.assetCode){
                                        if (item.profileValue > 0) {
                                            limitValue = item.profileValue;
                                        } else if (item.profileValue == 0) {
                                            limitValue = this.$t('basic.nolimit');
                                        } else if (item.profileValue == -1) {
                                            limitValue = this.$t('basic.noout');
                                        }
                                        limitArr[name].level1 = limitValue;
                                        break;
                                    }
                                }
                            });
                            commonService.assetConfigList({key:'WITHDRAWLEVEL_2'}).done((rep) => {
                                if (rep.code === window.CODE.SUCCESS) {
                                    rep.data.forEach((item, index) => {
                                        for(let name in limitArr){
                                            if(limitArr[name].assetCode === item.assetCode){
                                                if (item.profileValue > 0) {
                                                    limitValue = item.profileValue;
                                                } else if (item.profileValue == 0) {
                                                    limitValue = this.$t('basic.nolimit');
                                                } else if (item.profileValue == -1) {
                                                    limitValue = this.$t('basic.noout');
                                                }
                                                limitArr[name].level2 = limitValue;
                                                break;
                                            }
                                        }
                                    });
                                    console.log(limitArr)
                                    this.limitArr = limitArr;
                                    this.isLevelLoading = false;
                                } else {
                                    window.error(rep.msg)
                                }
                            })
                        } else {
                            window.error(rep.msg)
                        }
                    })
                } else {
                    window.error(rep.msg)
                }
            })
            */
        },

        formatAuditMessage(rep) {
            if (rep.data.userIdentification) {
                this.hasBasicAuthed = true;
                if (rep.data.userIdentification && rep.data.userIdentification.auditStatus) {
                    this.userIdentification.auditStatus = config[rep.data.userIdentification.auditStatus][this.$i18n.locale];

                    if (rep.data.userIdentification.auditMessageId) {
                        let reason = _.find(config.ID_FAIL_REASON, (item) => {
                            return item.ID == rep.data.userIdentification.auditMessageId
                        });
                        this.userIdentification.auditMessage = reason ? reason[this.$i18n.locale] : ''
                    } else {
                        this.userIdentification.auditMessage = '';
                    }
                } else if (rep.data.userIdentification && rep.data.userIdentification.auditStatus === null) {
                    this.userIdentification.auditStatus = config.UNVERIFIED[this.$i18n.locale];
                }

                if (rep.data.userResidence && rep.data.userResidence.auditStatus) {
                    this.userResidence.auditStatus = config[rep.data.userResidence.auditStatus][this.$i18n.locale];

                    if (rep.data.userResidence.auditMessageId) {
                        let reason = _.find(config.ADDR_FAIL_REASON, (item) => {
                            return item.ID == rep.data.userResidence.auditMessageId
                        });
                        this.userResidence.auditMessage = reason ? reason[this.$i18n.locale] : ''
                    } else {
                        this.userResidence.auditMessage = '';
                    }
                } else if (rep.data.userResidence && rep.data.userResidence.auditStatus === null) {
                    this.userResidence.auditStatus = config.UNVERIFIED[this.$i18n.locale];
                }

                //当状态为已提交Submitted时，应显示Identity verification application successfully.Please wait for customer service to approve
                if (rep.data.userIdentification && rep.data.userIdentification.auditStatus === config.AUTH_STATUS_SUBMITTED.KEY) {
                    this.userIdentification.auditMessage = this.$t('certification.IDauditMsg');
                }
                if (rep.data.userResidence && rep.data.userResidence.auditStatus === config.AUTH_STATUS_SUBMITTED.KEY) {
                    this.userResidence.auditMessage = this.$t('certification.ADDRauditMsg');
                }
            } else {
                this.hasBasicAuthed = false;
            }
        }
    }
});